'''
=========================
!/usr/bin/env python
_*_ coding:utf-8 _*_

@Time : 2020/7/20 14:40
@Coder: xh
@IDE  : PyCharm
=========================
'''
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPixmap
from ui_main import Ui_MainWindow
from showtab import showTab
from realtimeweather import RealTimeWeather
from getweather import GetWeather

class WeatherMain(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self, parent=None):
        """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(WeatherMain, self).__init__(parent)
        self.setupUi(self)
        self.initUi()
        self.kind = 0

    def initUi(self):
        # self.show_now_weather()
        pass
    @pyqtSlot(bool)
    def on_radioButton_toggled(self, checked):
        """
        实时天气
        """
        self.kind = 0
        self.show_now_weather()


    @pyqtSlot(bool)
    def on_radioButton_2_toggled(self, checked):
        """
        近3天天气
        """
        self.kind = 1
        self.show_future_weather()

    @pyqtSlot()
    def on_pushButton_clicked(self):
        """
        查询天气
        """
        if self.kind == 0:
            self.show_now_weather()
        elif self.kind == 1:
            self.show_future_weather()
            # t1,t2,t3,d1,d2,d3= self.show_future_weather()
            # self.tabWidget.addTab(t1, d1)
            # self.tabWidget.addTab(t2, d2)
            # self.tabWidget.addTab(t3, d3)

    def show_now_weather(self):
        try:
            city = self.lineEdit.text()
            w = GetWeather(0,city)
            weather_info = w.get_weather_info()
            msg = RealTimeWeather()
            msg.label_tt.setText(weather_info['tem'])
            msg.label_icon.setPixmap(QPixmap("./weather_api/icon/mango/" + weather_info['wea_img'] + ".png"))
            msg.label_ww.setText(weather_info['wea'])
            update = weather_info['update_time'] + "更新"
            self.tabWidget.clear()
            self.tabWidget.addTab(msg, update)
        except:
            msg = RealTimeWeather()
            msg.label_tt.setText('请输入要查询的城市，如：北京市输入北京，朝阳区输入朝阳')
            update = '错误提示'
            self.tabWidget.clear()
            self.tabWidget.addTab(msg, update)

    def show_future_weather(self):
        try:
            city = self.lineEdit.text()
            w = GetWeather(1,city)
            self.tabWidget.clear()
            weather_info = w.get_weather_info()
            tabwidget1 = showTab()
            tabwidget1.label_wb.setText(weather_info[1]['wea_day'])
            tabwidget1.label_tb.setPixmap(QPixmap("./weather_api/icon/mango/" + weather_info[1]['wea_img'] + ".png"))
            tabwidget1.label_wn.setText(weather_info[1]['wea_night'])
            # tabwidget0.label_tw.setPixmap(QPixmap("./res/" + weather0["code_night"] + ".png"))
            tabwidget1.label_tg.setText(weather_info[1]['tem1'])
            tabwidget1.label_tl.setText(weather_info[1]['tem2'])
            # tabwidget0.label_fx.setText(weather0["wind_direction"])
            tabwidget1.label_fl.setText(weather_info[1]['win_speed'])

            tabwidget2 = showTab()
            tabwidget2.label_wb.setText(weather_info[2]['wea_day'])
            tabwidget2.label_tb.setPixmap(QPixmap("./weather_api/icon/mango/" + weather_info[2]['wea_img'] + ".png"))
            tabwidget2.label_wn.setText(weather_info[2]['wea_night'])
            # tabwidget0.label_tw.setPixmap(QPixmap("./res/" + weather0["code_night"] + ".png"))
            tabwidget2.label_tg.setText(weather_info[2]['tem1'])
            tabwidget2.label_tl.setText(weather_info[2]['tem2'])
            # tabwidget0.label_fx.setText(weather0["wind_direction"])
            tabwidget2.label_fl.setText(weather_info[2]['win_speed'])

            tabwidget3 = showTab()
            tabwidget3.label_wb.setText(weather_info[3]['wea_day'])
            tabwidget3.label_tb.setPixmap(QPixmap("./weather_api/icon/mango/" + weather_info[3]['wea_img'] + ".png"))
            tabwidget3.label_wn.setText(weather_info[3]['wea_night'])
            # tabwidget0.label_tw.setPixmap(QPixmap("./res/" + weather0["code_night"] + ".png"))
            tabwidget3.label_tg.setText(weather_info[3]['tem1'])
            tabwidget3.label_tl.setText(weather_info[3]['tem2'])
            # tabwidget0.label_fx.setText(weather0["wind_direction"])
            tabwidget3.label_fl.setText(weather_info[3]['win_speed'])


            self.tabWidget.addTab(tabwidget1, weather_info[1]["date"])
            self.tabWidget.addTab(tabwidget2, weather_info[2]["date"])
            self.tabWidget.addTab(tabwidget3, weather_info[3]["date"])

            # return tabwidget1,tabwidget2,tabwidget3, weather_info[1]["date"],weather_info[2]["date"],weather_info[3]["date"]
        except:
            tabwidget1 = showTab()
            tabwidget1.label_wb.setText('请输入要查询的城市，如：北京市输入北京，朝阳区输入朝阳')
            self.tabWidget.addTab(tabwidget1,'错误提示')




if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    we  = WeatherMain()
    we.show()
    sys.exit(app.exec_())